//
//  RYCAlertView.m
//  testapp
//
//  Created by hemingway payegis on 13-1-21.
//  Copyright (c) 2013年 payegis. All rights reserved.
//

#import "RYCAlertView.h"

@interface RYCAlertView ()

@property (nonatomic, copy) void (^operation)(NSInteger);
@property (nonatomic, copy) void (^operationAfterDismiss)(NSInteger);

@end

@implementation RYCAlertView

- (void)showWithOperation:(void (^)(NSInteger clickedIndex))operation
{
    self.delegate = self;
    self.operation = operation;
    [self show];
}

- (void)showWithOperationAfterDismiss:(void (^)(NSInteger clickedIndex))operation
{
    self.delegate = self;
    self.operationAfterDismiss = operation;
    [self show];
}

+ (void)showHint:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString *)cancelButtonTitle operation:(void (^)(NSInteger clickedIndex))operation
{
    RYCAlertView* alertView = [[RYCAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    [alertView showWithOperation:operation];
}

+ (void)showHint:(NSString*)title message:(NSString*)message operation:(void (^)(NSInteger clickedIndex))operation
{
    [RYCAlertView showHint:title message:message cancelButtonTitle:@"确定" operation:operation];
}

+ (void)showConfirm:(NSString*)title message:(NSString*)message operation:(void (^)(NSInteger clickedIndex))operation
{
    RYCAlertView* alertView = [[RYCAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView showWithOperation:operation];
}

+ (void)selectBox:(NSString*)aTitle message:(NSString*)aMessage operation:(void (^)(NSInteger clickedIndex))operation cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,...
{
    RYCAlertView* alertView = nil;
    
    if(otherButtonTitles)
    {
        alertView = [[RYCAlertView alloc] initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
        
        NSString* arg;
        va_list argList;
        NSMutableArray* arrArg = [[NSMutableArray alloc] initWithCapacity:5];
        va_start(argList,otherButtonTitles);
        while (1)
        {
            arg = va_arg(argList,NSString*);
            if (arg)
            {
                [arrArg addObject:arg];
            }
            else
            {
                break;
            }
        }
        va_end(argList);
        
        for (NSString* title in arrArg)
        {
            [alertView addButtonWithTitle:title];
        }
    }
    else
    {
        alertView = [[RYCAlertView alloc] initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    }
    [alertView showWithOperation:operation];
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (_operation)
    {
        _operation(buttonIndex);
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (_operationAfterDismiss) {
        _operationAfterDismiss(buttonIndex);
    }
}


@end
